<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/head.jsp" %>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style type="text/css">
#content {
        position: absolute;
        top: 0;
        left: 240px;
        right: 10px;
        bottom: 0;
        box-sizing: border-box;
    }
    ul.ztree {
       width: 230px;
    }
</style>

</head>
<body class="iframe-page">
  <!-- 左侧菜单显示 -->
	<div class="tree-wrap">
	    <div class="tree-title">商品分类列表</div>
	    <div class="tree-content">
		  	<!-- 拼接树 -->
		    <ul id="categoryTree" class="ztree"></ul>
		</div>
	</div>
    
    <!-- 右侧拼接页面 -->
	<div id="content">
	    <iframe id="categoryFrame" frameborder="0" style="width: 100%;height: 100%" src="<%=request.getContextPath()%>/mall/system/goods/goodscategory/initAdd?codeid=000&pname=全部"></iframe>
	</div>
    
    <!-- 鼠标右键 -->
    <div id="resourceMenu" style="width:140px;">
	      <div id="add">新建子节点</div>
	      <div id="edit">编辑节点</div>
	      <div id="delete">删除节点</div>
    </div>
    
<script>

	//请求url
	var GOODS_CATEGORY_URL = {
		INIT_MAIN: basePath + '/mall/system/goods/goodscategory/init',
   		INIT_ADD: basePath + '/mall/system/goods/goodscategory/initAdd',
   		INIT_EDIT: basePath + '/mall/system/goods/goodscategory/initEdit',
   		INIT_VIEW: basePath + '/mall/system/goods/goodscategory/initView',
   		DELETE: basePath + '/mall/system/goods/goodscategory/del',
		INIT_GOODSCATEGORY: basePath + '/mall/system/goods/goodscategory/initGoodscategory',
		GET_TREE: basePath + "/sys/inner/getCategoryTree"
	};

	var categoryFrame = document.getElementById("categoryFrame"),ztree;
	
	
	//初始化组织机构树
	function initOrgTree(treeId, url,idKey, pIdKey) {
		var setting = {
	        data: {
	            simpleData: {
	                enable: true,
	                idKey: idKey,
                    pIdKey: pIdKey
	            }
	        },
	        edit: {
	            enable: false,
	            drag: {
	                isMove: false
	            }
	        },
	        callback: {
	            onClick: selectUserByOrgCode,
	            onRightClick: showResourceMenu
	        }
	    };
	    
	    $.sajax({
	        url: url,
	        onLoadSuccess: function(data) {
	            ztree = $.fn.zTree.init($('#' + treeId),setting,data.data);
	            expandNode();
	        }
	    });
	}
	
	$(function () {
		//初始化左侧组织机构树
		initOrgTree('categoryTree', GOODS_CATEGORY_URL.GET_TREE, 'codeid', 'pid');
		
		 $('#resourceMenu').menu({
             onClick: function(item){
             	if (item.id == 'add') {
             		addResource(currentNode);
             	} else if (item.id == 'edit') {
             		editResource(currentNode);
             	} else if (item.id == 'delete') {
             		deleteResource(currentNode);
             	}
             }
         }); 
	});
	
	
	
	// 展开树节点
	function expandNode() {
		var nodeCode = '${param.expandId}',node;
		if (nodeCode) {
		   // 获取需要自动展开的节点
		   node = ztree.getNodeByParam('codeid', nodeCode, null);
		} else {
		   // nodeId为空,展开根节点
		   node = ztree.getNodeByParam('pid', null, null);
		}
		// 如果该节点不包含子节点,则展开其父节点
		if (node && !node.isParent) {
		  node = node.getParentNode(); 
		}
		ztree.expandNode(node, true, false, true);
	}
	
	 // 查看资源
    function viewResource(event, treeId, treeNode) {
    	var url = GOODS_CATEGORY_URL.INIT_VIEW+'?id=' + treeNode.id;
    	if (!treeNode.pId) {
    		url = GOODS_CATEGORY_URL.INIT_STAT;
    	}
    	categoryFrame.src = url;
    }
    // 添加资源
    function addResource(node) {
    	 /* 暂时没有限制 */
	   	  if (node.codeid.length == 9) {
	   		 $.sdialog({
	   			 type: 'warn',
	   			 msg: '无法添加节点,商品分类最多二级'
	   		 });
	   		 return false;
	   	 } else{
		   	categoryFrame.src = GOODS_CATEGORY_URL.INIT_ADD + '?codeid=' + node.codeid + '&pname=' + node.name; 
	   	 }
    	 
	   	 
    }
    
    // 编辑资源
    function editResource(node) {
    	categoryFrame.src = GOODS_CATEGORY_URL.INIT_EDIT+'?id='+node.id
    }
    
    //删除资源
    function deleteResource(node) { 
    	$.sdialog({
    		type: 'confirm',
    		msg: '您确定要删除<'+node.name+'>节点吗? 删除时会将该节点下的子节点一并删除',
    		onConfirm: function(result) {
    			if(result){
  			     $.sajax({
    	                url: GOODS_CATEGORY_URL.DELETE+'?id='+node.id,
    	                onLoadSuccess: function (data) {
    	                	$.stip({
    	                       type:'ok',//显示类型 有info、 ok、 danger
    	                       text: '删除成功！',
    	                       onBeforeHide: function () {
    	                    	  refreshPage(node.pId); 
    	                      }
    	                   });
    	                }
    	            });
    			}
    		}
    	});
    	
    }
	
    // 显示右键菜单
    function showResourceMenu(event, treeId, treeNode) {
		currentNode = treeNode;
		ztree.selectNode(currentNode);
        $('#resourceMenu').menu('show',{left:event.pageX, top: event.pageY});
    }
    
    // 刷新页面, nodeId为初始化页面时需要自动展开的节点ID
    function refreshPage(nodeId, continueAdd, level) {
   	 nodeId = nodeId || '';
    	 if (continueAdd){
    		var node = ztree.getNodeByParam("pid", nodeId, null);
    		ztree.setting.async.url = GOODS_CATEGORY_URL.GET_TREE + "?pid=" + nodeId;
    		node.isParent = true,
           ztree.updateNode(node);
    		ztree.reAsyncChildNodes(node, "refresh");
    	 } else {
    	    location.href = GOODS_CATEGORY_URL.INIT_MAIN+'?pid='+nodeId;
    	 }
    }
    
    
	//点击组织机构树查询用户
	function selectUserByOrgCode(event, treeId, treeNode) {
		
		url = GOODS_CATEGORY_URL.INIT_VIEW + "?id=" + treeNode.id;
		categoryFrame.src = url; 
		//选中节点
		ztree.selectNode(treeNode);
	}
	
	//阻止默认的右键菜单
   /* function forbidRightMenu(event, treeId, treeNode) {
		document.oncontextmenu = function (){
		   return true;
		};
	}  */
	
	
</script>
</body>
</html>